
import { gethousedetail, getfavoritehouse, addhouselike, deletehouselike } from '@/api/user'
// import { favoriates } from '@/api/user'
const state = {
  obj: {},
  isshowcollect: false,
  center: { lat: 0, lng: 0 },
  show: false
}

const mutations = {
  loadgethousedetail (state, payload) {
    state.obj = payload
    state.center.lng = payload.coord.longitude
    state.center.lat = payload.coord.latitude
    state.show = true
  },
  loadgetfavoritehouse (state, payload) {
    state.isshowcollect = payload
  },
  deletehouses (state) {
    state.isshowcollect = false
  },
  addhouses (state) {
    state.isshowcollect = true
  }
}
const actions = {
  async gethousedetails (context, payload) {
    const res = await gethousedetail(payload)
    // console.log(res.data.body.coord)
    context.commit('loadgethousedetail', res.data.body)
  },
  async getfavoritehouses (context, payload) {
    const res = await getfavoritehouse(payload)
    context.commit('loadgetfavoritehouse', res.data.body.isFavorite)
  },
  async addordeletefavorite (context, payload) {
    if (context.state.isshowcollect) {
      try {
        await deletehouselike(payload)
        context.commit('deletehouses')
      } catch (error) {
        console.log(error)
      }
    } else {
      try {
        await addhouselike(payload)
        context.commit('addhouses')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
const getters = {

}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
